Hi everybody!
I hope you all had a merry christmas... =)
....because I need your help!! >.<
I'm new to C and I'm getting to like this language more and more at every step I take, but I keep banging my head against several walls.
Today it's the "malloc/free" wall.
I've built an exercise to learn how to pass values and memory pointers to functions. It's more like a simple "test my knowledge" thing, actually, but still good enough to understand and exercise logic.
The exercise is very simple:
- given two "int" variables, pass their values to a function that returns their sum; then print it on the screen
- given a structure containing three "int" elements, allocate memory and pass its pointer to a function that calculates the product of the first two elements and saves it in the third one; then print it on the screen
No problems AT ALL with the first part. Happy!!
The second part, instead, works great until I use "free" to de-allocate memory.
For a matter of speed, I've written it partly with italian words (my language), but I'll translate the "printf" messages so that you know what's happening.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//----------------------------
//----------------------------
unsigned long secondary();
int pass_struct ();
struct test
{
int numero1;
int numero2;
int prodotto;
};
//----------------------------
//----------------------------
//----------------------------
//----------------------------
int main(void)
{
//---------------------------- PASSAGGIO DI DUE VALORI SEPARATI COME PARAMETRI PER UNA CHIAMATA AD UNA FUNZIONE
unsigned long a, b, result;
a = 2495383;
b = 34934059;
result = secondary (a, b);
printf("a = %lu\nb = %lu\n\na+b = %lu\n\n", a, b, result);
//----------------------------
//---------------------------- PASSAGGIO DI UN PUNTATORE STRUTTURA COME PARAMETRO PER UNA CHIAMATA AD UNA FUNZIONE
struct test * oggetto1;
oggetto1 = malloc(sizeof(oggetto1));
printf("Allocated oggetto1 at the address %p\n\n", oggetto1);
oggetto1->numero1 = 2;
oggetto1->numero2 = 7;
oggetto1->prodotto = 0;
oggetto1 = pass_struct(oggetto1); //<<-- Actually I shouldnt need to assign the returned value, it would be enough to write "pass_struct(oggetto1)"; right?
printf("Executed call for pass_struct. Address oggetto1: %p\n\n\n", oggetto1);
printf("numero1 = %i\nnumero2 = %i\nnumero1 * numero2 = %i\n\n", oggetto1->numero1, oggetto1->numero2, oggetto1->prodotto);
free(oggetto1); //<<-- Why the heck does it give an error!?!?!??!? >.<
//----------------------------
return 0;
}
//----------------------------
//----------------------------
//----------------------------
//----------------------------
unsigned long secondary (unsigned long uno, unsigned long due)
{
return uno + due;
}
//----------------------------
//----------------------------
//----------------------------
//----------------------------
int pass_struct (struct test * pointer)
{
pointer->prodotto = pointer->numero1 * pointer->numero2;
return pointer; //<<-- Absolutely useless, since the pointer doesnt change and it remains in the main function, aint it so? =o
}
//----------------------------
//----------------------------
The error comes out randomly, not at every execution; clear sign that it's bound to memory addresses, as if the address I'm trying to free is no longer reserved and gets used by another application.
I've tried to analyze the whole thing excluding the most common errors like scopes, re-assignements and the likes.
As you can see I've even set an extra "printf" after the call for the external function to ensure the address inside the "oggetto1" variable is still the same as before.
I've already (made and) done another exercise that finds all the prime numbers between 3 and "n" (value given by user) and memorizes them in a "list" of structures; then frees all the allocated memory through the "free" function, but I get absolutely no errors, there. Argh!! >.<
The "good side" is that it finds all the prime numbers between 3 and 40.000.000 in about 14 seconds. (Intel i7 920 @ 4.00GHz)
The "bad side" is that it's written in a single block of code (awful!!), while I'd like to separate each routine in a different header file, but first I want to be sure I'm able to pass data safely between separate functions.
I guess this error has something to do with passing the pointer to another function, but I cant understand how.
Please, help!! =|
PS: about the address I "return" from the "pass_struct" function and that I assign to "oggetto1", I've also re-written it to return absolutely nothing and not assign it to "oggetto1", but the error still happens.
Just to be clear:
- the function call would be simply "struct_pass(oggetto1);"
- at the end of the "struct_pass" function there would be a simple "return 0;"
PPS: I'm using "QT Creator" ver. 2.4.1, as compiler